package all.com.jinyongqunxiax.modules;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.randy.fundation.ImageUitls;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import java.util.Arrays;
import java.util.List;

import all.com.jinyongqunxiax.R;
import all.com.jinyongqunxiax.model.mod.manager.Mod;
import all.com.jinyongqunxiax.model.mod.manager.ModManager;
import all.com.jinyongqunxiax.utility.JxThreadPool;
import all.com.jinyongqunxiax.view.JxHud;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.Animations;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.Audios;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.BattleBg;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.CGs;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.Heads;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.Icons;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.Items;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.Maps;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.Scripts;
import static all.com.jinyongqunxiax.model.mod.manager.Mod.Type.lua;

public class ModDetailActivity extends AppCompatActivity {

    SlimAdapter adapter = null;

    // BattleBg 战斗背景 参考规格：1140*640
    // CGs TOUCH背景
    // Heads 人物头像 参考规格：128*128，带alpha通道
    // Icons 图标 参考规格：64*64，带底
    // items 物品 参考规格：128*128，带底
    // Maps 地图场景 参考规格：1140*640

    List<ViewModel> viewModels = Arrays.asList(
        new ViewModel("Animations", Animations),
        new ViewModel("Audios", Audios),
        new ViewModel("BattleBg", BattleBg),
        new ViewModel("CGs", CGs),
        new ViewModel("Heads", Heads),
        new ViewModel("Icons", Icons),
        new ViewModel("items", Items),
        new ViewModel("lua", lua),
        new ViewModel("Maps", Maps),
        new ViewModel("Scripts", Scripts));
    Options         options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_activity);

        Intent intent = getIntent();
        options = new Options(intent);

        setTitle(options.name);

        RecyclerView recyclerView = findViewById(R.id.detail_components_listView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        adapter = SlimAdapter.create().register(R.layout.detail_component_item, new SlimInjector<ViewModel>() {
            @Override
            public void onInject(@NonNull ViewModel viewModel, @NonNull IViewInjector injector) {
                injector.text(R.id.detail_components_title, viewModel.title);
                injector.clicked(R.id.detail_components_title, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openComponent(viewModel);
                    }
                });
            }
        }).attachTo(recyclerView);

        TextView textView = findViewById(R.id.detail_activity_error_view);
        Object   hud      = JxHud.loading(this, "");

        JxThreadPool.shared().post(new JxThreadPool.JxThreadRunable() {
            @Override
            public String name() {
                return "mod: load";
            }

            @Override
            public void run() {
                String message = "";
                try {
                    Mod mod = ModManager.shared().get(options.root);
                    message = mod.load();
                } catch (Exception e) {
                    e.printStackTrace();
                    message = e.getMessage();
                }

                String finalMessage = message.trim();
                runOnUiThread(() -> {
                    adapter.updateData(viewModels);
                    ((View) textView.getParent()).setVisibility(
                        TextUtils.isEmpty(finalMessage) ? View.GONE : View.VISIBLE);
                    textView.setText(finalMessage);
                    JxHud.hideLoading(hud);
                });
            }
        });
    }

    void openComponent(ViewModel viewModel) {
        switch (viewModel.type) {
            case Icons:
            case Maps:
            case Heads:
            case Items:
            case BattleBg:
            case CGs:
                ImageUitls.ImageSize size = Mod.suggestImageSize(viewModel.title);
                if (size != null) {
                    Intent intent1 = ImageListActivity.Options.intent(
                        ModDetailActivity.this, options.root, viewModel.title,
                        size.width, size.height);
                    startActivity(intent1);
                } else {
                    JxHud.alert("cannot get suggest size for %s", viewModel.title);
                }
                break;
            case Scripts:
                Intent intent2 = ScriptsActivity.Options.intent(
                    ModDetailActivity.this, options.root, viewModel.title);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }

    static class ViewModel {
        String   title;
        Mod.Type type;

        ViewModel(String title, Mod.Type type) {
            this.title = title;
            this.type = type;
        }
    }

    public static class Options {
        String name;
        String root;

        Options(Intent intent) {
            name = intent.getStringExtra("name");
            root = intent.getStringExtra("mod");
        }

        public static Intent intent(Context context, String name, String root) {
            Intent intent = new Intent(context, ModDetailActivity.class);
            intent.putExtra("name", name);
            intent.putExtra("mod", root);
            return intent;
        }
    }
}
